import threading
import time

g_num = 0

def test1(count):
    global g_num
    for i in range(count):
       mutex.acquire()
       g_num += 1
       mutex.release()
    print('in test1 g_num=%d' % g_num)

def test2(count):
    global g_num
    for i in range(count):
        mutex.acquire()
        g_num += 1
        mutex.release()
    print('in test2 g_num=%d' % g_num)

mutex = threading.Lock()

def main():

    t1 = threading.Thread(target=test1,args=(1000000,)) # target指定子线程的函数
    t2 = threading.Thread(target=test2,args=(1000000,))
    t1.start() 
    t2.start()

    time.sleep(2)
    print("g_num=%d" % g_num)

main()